<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Explaining;
use app\common\model\Manual;
use app\common\model\Notice;
use app\common\model\QuestionGroup;
use app\common\model\Supervision;
use app\common\model\User;
use app\common\model\UserCivil;
use app\common\model\UserClockIn;
use app\common\model\UserManual;
use app\common\model\UserSupervisor;
use think\Db;
use think\Request;

/**
 * 教师端内容
 */
class Teacher extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**------------------申论管理---------------------------------------------------- */
    /**
     * 申论列表
     * @return void 
     */
    public function explaining()
    {
        $teacher_id = $this->auth->id;
        $status = $this->request->param('status', 1); # 状态:1=待批改,2=已批改
        if (!in_array($status, [1, 2])) {
            $this->error('状态错误');
        }
        $list = (new Explaining())->getList(['teacher_id' => $teacher_id, 'status' => $status]);
        $this->success('获取申论列表成功', $list);
    }

    /**
     * 申论详情
     */
    public function explainingDetail()
    {
        $explaining_id = $this->request->param('explaining_id');
        $info = (new Explaining())->where('id', $explaining_id)->find(); # 申论详情
        $user = (new User())->where('id', $info->user_id)->field('id,nickname,avatar,mobile')->find();
        $info->user = $user;
        $this->success('获取申论详情成功', $info);
    }

    /**
     * 申论批改
     * @return void 
     */
    public function explainingSave()
    {
        $explaining_id = $this->request->param('explaining_id');
        $correction = $this->request->param('correction');
        $photo = $this->request->param('photo');
        if (!$correction) $this->error("批改内容");
        $info = (new Explaining())->where('id', $explaining_id)->find();
        if (!$info) {
            $this->error('申论不存在');
        }
        $teacher_id = $this->auth->id;
        if ($info->teacher_id != $teacher_id) {
            $this->error('无权限批改');
        }
        if ($info->status != 1) {
            $this->error('申论状态错误');
        }
        $info->status = 2;
        $info->photo = $photo;
        $info->correction = $correction;
        $info->updatetime = time();
        $info->save();
        $this->success('批改成功');
    }
    /**------------------公考管理---------------------------------------------------- */

    /**
     * 公考用户列表
     * @return void 
     */
    public function civilUserList()
    {
        $teacher_id = $this->auth->id;
        if (!$teacher_id) {
            return $this->error('老师未登录');
        }
        // 使用模型方法获取老师的所有学生列表
        $students = UserCivil::getTeacherStudents($teacher_id);
        if ($students && count($students) > 0) {
            return $this->success('获取学生列表成功', [
                'total_count' => count($students),
                'list' => $students
            ]);
        } else {
            return $this->success('暂无学生数据', [
                'total_count' => 0,
                'list' => []
            ]);
        }
    }

    /**
     * 获取学生的详细公考资源列表
     * @return void 
     */
    public function getStudentCivilList()
    {
        $teacher_id = $this->auth->id;
        $user_id = $this->request->param('user_id');
        if (!$teacher_id) {
            return $this->error('老师未登录');
        }
        if (!$user_id) {
            return $this->error('学生ID不能为空');
        }
        // 验证该学生是否属于当前老师
        $student_exists = UserCivil::where('teacher_id', $teacher_id)
            ->where('user_id', $user_id)
            ->find();
        if (!$student_exists) {
            return $this->error('该学生不属于您管理');
        }
        // 获取学生的详细公考资源列表
        $civil_list = UserCivil::getStudentCivilDetails($teacher_id, $user_id);
        // 获取学生基本信息
        $student_info = User::where('id', $user_id)->field('id, nickname, avatar, mobile')->find();
        return $this->success('获取学生公考资源成功', [
            'student_info' => $student_info,
            'total_count' => count($civil_list),
            'list' => $civil_list
        ]);
    }

    /**公考详情 */
    public function civilDetail()
    {
        $id = $this->request->param('id');
        $info = (new UserCivil())->alias('uc')
            ->join('civil c', 'uc.civil_id = c.id', 'LEFT')
            ->field('uc.*,c.title as civil_name,c.label as civil_label,c.image as civil_image')
            ->where('uc.id', $id)->find();
        if (!$info) {
            $this->error('公考不存在');
        }
        if ($info['civil_label']) {
            $info['civil_label'] = explode(',', $info['civil_label']);
        }
        $this->success('获取公考详情成功', $info);
    }

    /**公考备注 */
    public function civilRemark()
    {
        $id = $this->request->param('id');
        $remark = $this->request->param('remark');
        if (!$id || !$remark) {
            $this->error('ID和备注不能为空');
        }
        $info = (new UserCivil())->where('id', $id)->find();
        if (!$info) {
            $this->error('公考不存在');
        }
        $info->remark = $remark;
        $info->save();
        $this->success('公考备注成功');
    }


    /**------------------会员督学---------------------------------------------------- */
    /**
     * 督学 学生列表
     * @return void
     */
    public function supervisionStudentList()
    {
        $teacher_id = $this->auth->id;
        $list = (new UserSupervisor())->getTeacherStudents($teacher_id);
        $this->success('督学学生列表', $list);
    }

    /**
     * 获取学生督学信息
     * @return void
     */
    public function supervisionList()
    {
        $type = $this->request->param('type'); #1=待批改,2=已批改
        if (!in_array($type, [1, 2])) {
            $this->error("参数错误");
        }
        $user_id = $this->request->param('user_id'); # 学生id
        # 查询学生信息
        $user = (new User())->where('id', $user_id)->field('id,nickname,avatar,mobile')->find();
        if (empty($user)) {
            $this->error("用户不存在");
        }
        $list = (new UserSupervisor())->getSupervisionList($user_id, $type);
        $this->success('督学列表', ['user' => $user, 'list' => $list]);
    }



    /**
     * 督学详情
     * @return void
     */
    public function supervisionDetail()
    {
        $id = $this->request->param('id');
        $info = Db::name('supervision')->where('id', $id)->find();
        if (!$info) {
            $this->error('督学不存在');
        }
        $info['supervision_pic'] = !empty($info['supervision_pic']) ? explode(',', $info['supervision_pic']) : [];
        $info['student_pic'] = !empty($info['student_pic']) ? explode(',', $info['student_pic']) : [];
        $info['teacher_pic'] = !empty($info['teacher_pic']) ? explode(',', $info['teacher_pic']) : [];
        $this->success('督学详情', $info);
    }

    /**
     * 老师督学学生
     * @return void
     */
    public function supervisionAdd(Request $request)
    {
        $teacher_id = $this->auth->id;
        $params = $request->param();
        $user = Db::name('user')->where('id', $params['user_id'])->find();
        if (!$user) {
            $this->error('用户不存在');
        }
        if (!$params['start_time'] || !$params['end_time']) {
            $this->error('开始时间和结束时间不能为空');
        }
        $start_time = strtotime($params['start_time']);
        $end_time = strtotime($params['end_time']);
        if ($start_time > $end_time) {
            $this->error('开始时间不能大于结束时间');
        }
        if (!$params['supervision_content']) {
            $this->error('督学内容不能为空');
        }
        $data = [
            'user_id' => $user['id'],
            'teacher_id' => $teacher_id,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'supervision_content' => $params['supervision_content'],
            'supervision_pic' => $params['supervision_pic'],
            'createtime' => time(),
        ];
        $supervision_id = Db::name('supervision')->insertGetId($data);
        if ($supervision_id) {
            # 通知用户 督学信息
            $arr_content = [
                'supervision_id' => $supervision_id,
            ];
            (new Notice())->createSupervisionNotice($user['id'], '您有新的督学信息', $params['supervision_content'], $params['supervision_content'], $arr_content);
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }


    /**
     * 老师批改
     * @param \think\Request $request
     * @return void
     */
    public function supervisionCorrect(Request $request)
    {
        $params = $request->param();
        $info = (new Supervision())->where('id', $params['id'])->find();
        if (!$info) {
            $this->error('未找到该条数据');
        }
        $info->teacher_content = $params['teacher_content'];
        $info->teacher_pic = $params['teacher_pic'];
        $info->updatetime = time();
        $info->save();
        $this->success('批改成功');
    }


    /**-------------一对一答疑-------------------------------------------------------- */

    /**
     * 答疑列表
     * @return void
     */
    public function listManual()
    {
        $teacher_id = $this->auth->id;
        $status = $this->request->param('status', 0);
        $list = (new Manual)->getListManual(['status' => $status],$teacher_id);
        $this->success('', $list);
    }

    /**
     * 去答疑
     * @return void
     */
    public function gtoqa()
    { 
        $id = $this->request->param('id');
        $correction = $this->request->param('correction');
        $photo = $this->request->param('photo','');
        $info = (new Manual)->where('id', $id)->find();
        if (!$info) {
            $this->error('未找到该条数据');
        }
        $info->correction = $correction;
        $info->photo = $photo;
        $info->status = 2;
        $info->updatetime = time();
        $info->save();
        $this->success('答疑成功');
    }

    /**
     * 答疑详情
     * @return void
     */
    public function datalistManual()
    { 
        $id = $this->request->param('id');
        $info  = (new Manual)->where('id', $id)->find();
        if (!$info) {
            $this->error('未找到该条数据');
        }
        $info->user = (new User)->where('id', $info->user_id)->find();
        $this->success('成功', $info);
    }


    /**
     * 答疑学生列表
     * @return void
     */
    public function userManualList()
    {
        $teacher_id = $this->auth->id;
        $list = (new UserManual())->alias('a')
        ->join('user b','a.user_id = b.id')
        ->field('a.id,a.user_id,b.nickname,b.avatar')
        ->where('a.teacher_id',$teacher_id)
        ->select();
        $this->success('成功', $list);
    }



















    /**
     * 反馈教师端
     * @return void
     */
    public function feedbackTeacherList()
    {
        $list = Db::name('feedback')->field('id,createtime,content')->where('teacher_is', '1')->select();
        $this->success('OK', $list);
    }
}
